const TokenKey = 'token'
const UserInfoKey = 'userInfo'
const UserMenusKey = 'userMenus'
const AllowedPathsKey = 'allowedPaths'
const RoutesAddedKey = 'routesAdded'

export function getToken() {
  return localStorage.getItem(TokenKey)
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token)
}

export function removeToken() {
  return localStorage.removeItem(TokenKey)
}

// 用户信息相关
export function getUserInfo() {
  const userInfo = localStorage.getItem(UserInfoKey)
  return userInfo ? JSON.parse(userInfo) : null
}

export function setUserInfo(userInfo) {
  return localStorage.setItem(UserInfoKey, JSON.stringify(userInfo))
}

export function removeUserInfo() {
  return localStorage.removeItem(UserInfoKey)
}

// 菜单权限相关
export function getUserMenus() {
  const menus = localStorage.getItem(UserMenusKey)
  return menus ? JSON.parse(menus) : []
}

export function setUserMenus(menus) {
  return localStorage.setItem(UserMenusKey, JSON.stringify(menus))
}

export function removeUserMenus() {
  return localStorage.removeItem(UserMenusKey)
}

// 允许访问的路径相关
export function getAllowedPaths() {
  const paths = localStorage.getItem(AllowedPathsKey)
  return paths ? JSON.parse(paths) : []
}

export function setAllowedPaths(paths) {
  return localStorage.setItem(AllowedPathsKey, JSON.stringify(paths))
}

export function removeAllowedPaths() {
  return localStorage.removeItem(AllowedPathsKey)
}

// 路由是否已添加相关
export function getRoutesAdded() {
  const routesAdded = localStorage.getItem(RoutesAddedKey)
  return routesAdded === 'true'
}

export function setRoutesAdded(added) {
  return localStorage.setItem(RoutesAddedKey, added.toString())
}

export function removeRoutesAdded() {
  return localStorage.removeItem(RoutesAddedKey)
}

// 清除所有用户相关数据
export function clearAllUserData() {
  removeToken()
  removeUserInfo()
  removeUserMenus()
  removeAllowedPaths()
  removeRoutesAdded()
}
